#! -*- coding: utf-8 -*-

from Tkinter import *

class MyApp:
    def __init__(self, parent):

        self.myParent = parent
        self.myParent.geometry("240x200")
        self.myParent.title('El Efecto Mariposa')

        ### Our topmost frame is called myContainer1
        self.myContainer1 = Frame(parent) ###
        self.myContainer1.pack()
        self.centerWindow()

        #------ constants for controlling layout ------
        button_width = 8      ### (1)

        button_padx = "2m"    ### (2)
        button_pady = "1m"    ### (2)

        buttons_frame_padx =  "3m"   ### (3)
        buttons_frame_pady =  "2m"   ### (3)
        buttons_frame_ipadx = "3m"   ### (3)
        buttons_frame_ipady = "1m"   ### (3)
        # -------------- end constants ----------------

        # buttons frame
        self.buttons_frame = Frame(self.myContainer1) ###
        self.buttons_frame.pack(
            side=TOP,   ###
            ipadx=buttons_frame_ipadx,
            ipady=buttons_frame_ipady,
            padx=buttons_frame_padx,
            pady=buttons_frame_pady,
            )

        # top frame
        self.top_frame = Frame(self.myContainer1)
        self.top_frame.pack(side=TOP,
            fill=BOTH,
            expand=YES,
            )  ###

        # now we add the buttons to the buttons_frame
        self.button1 = Button(self.buttons_frame, command=self.button1Click)
        self.button1.configure(text="Instalar TBE", background= "green")
        self.button1.focus_force()
        self.button1.configure(
            width=button_width,  ### (1)
            padx=button_padx,    ### (2)
            pady=button_pady     ### (2)
            )

        self.button1.pack(side=LEFT)
        self.button1.bind("<Return>", self.button1Click_a)

        self.button2 = Button(self.buttons_frame, command=self.button2Click)
        self.button2.configure(text="Salir", background="red")
        self.button2.configure(
            width=button_width,  ### (1)
            padx=button_padx,    ### (2)
            pady=button_pady     ### (2)
            )

        self.button2.pack(side=RIGHT)
        self.button2.bind("<Return>", self.button2Click_a)

    def centerWindow(self):
        w = 290
        h = 150
        sw = self.myParent.winfo_screenwidth()
        sh = self.myParent.winfo_screenheight()
        x = (sw - w)/2
        y = (sh - h)/2
        self.myParent.geometry('%dx%d+%d+%d' % (w, h, x, y))


    def button1Click(self):
        if self.button1["background"] == "green":
            self.button1["background"] = "yellow"
        else:
            self.button1["background"] = "green"

        import os
        import tkMessageBox
        print 'Se instalará TBE en tu sistema'
        os.system('mkdir $HOME/tbe-educar')
        os.system('unzip -d $HOME/tbe-educar/ TBE.zip')
        os.system('ln -s $HOME/tbe-educar/./tbe $HOME/Escritorio/tbe')
        print 'Instalación finalizada, cliquea en "Salir"'
        tkMessageBox.showinfo("Finalizado","Cliquea en 'Salir'")

    def button2Click(self):
        self.myParent.destroy()

    def button1Click_a(self, event):
        self.button1Click()

    def button2Click_a(self, event):
        self.button2Click()


root = Tk()
myapp = MyApp(root)
root.mainloop()
